package com.fjec.market.rabbitmq.provider;

import com.fjec.common.protocol.FjecFruitsProto;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class PreparePayProvider {

    @Value("${fjec.paid.preparepay.exchange.name}")
    private String exchangeName;

    @Value("${fjec.paid.preparepay.router.key}")
    private String routingKey;


    @Resource(name = "preparePayAmqpTemplate")
    private RabbitTemplate preparePayAmqpTemplate;

    public void sendPreparePay(String subOpenId, String outTradeNo, String shopDate, String subject,
                               String totalAmount, String sellerId, String sellerName, Integer paymentType) {
        FjecFruitsProto.PreparePay preparePay = FjecFruitsProto.PreparePay.newBuilder()
                .setSubOpenId(subOpenId)
                .setOutTradeNo(outTradeNo)
                .setShopDate(shopDate)
                .setSubject(subject)
                .setTotalAmount(totalAmount)
                .setSellerId(sellerId)
                .setSellerName(sellerName)
                .setPaymentType(paymentType)
                .build();

        FjecFruitsProto.FjecFruitsMsg msg = FjecFruitsProto.FjecFruitsMsg.newBuilder().setPreparePay(preparePay).build();
        CorrelationData correlationData = new CorrelationData(subOpenId + "|" + outTradeNo + "|" + sellerId);
        preparePayAmqpTemplate.convertAndSend(exchangeName, routingKey, msg.toByteArray(), correlationData);
    }
}
